package cn.niit.controller;


import cn.niit.entity.Department;
import cn.niit.entity.Major;
import cn.niit.entity.Teacher;
import cn.niit.entity.query.TeacherQuery;
import cn.niit.service.DepartmentService;
import cn.niit.service.MajorService;
import cn.niit.service.TeacherService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/teacher")
public class TeacherController {
    @Resource
    private TeacherService teacherService;
    @Resource
    private DepartmentService departmentService;
    @Resource
    private MajorService majorService;
    @GetMapping("/query")
    public String queryByTeacher(Model model, TeacherQuery teacherQuery) {
        PageInfo<Teacher> teacherPageInfo = teacherService.queryByTeacher(teacherQuery);
        List<Department> departmentList=departmentService.queryAllDepartmentList();
        List<Major> majorList=majorService.queryAllMajorList();
        model.addAttribute("majorList",majorList);
        model.addAttribute("departmentList",departmentList);
        model.addAttribute("page", teacherPageInfo);
        return "/teacher/teacher";
    }
    @GetMapping("/select")
    public String select(Model model,TeacherQuery teacherQuery){
        PageInfo<Teacher> teacherPageInfo = teacherService.select(teacherQuery);
        List<Department> departmentList=departmentService.queryAllDepartmentList();
        List<Major> majorList=majorService.queryAllMajorList();
        model.addAttribute("majorList",majorList);
        model.addAttribute("departmentList",departmentList);
        model.addAttribute("page", teacherPageInfo);
        return "/teacher/teacher";
    }

    @GetMapping("/edit/{id}")
    public String toEdit(@PathVariable Integer id, Model model) {
        List<Department> departmentList=departmentService.queryAllDepartmentList();
        List<Major> majorList=majorService.queryAllMajorList();

        model.addAttribute("majorList",majorList);
        model.addAttribute("departmentList",departmentList);
        model.addAttribute("teacher", teacherService.queryTeacherById(id));
        return "/teacher/editTeacher";
    }

    @PostMapping("/edit")
    public String edit(Teacher teacher) {
        TeacherQuery teacherQuery = new TeacherQuery();
        Integer id = teacher.getId();
        teacherQuery.setTeacherName(teacher.getTeacherName());
        PageInfo<Teacher> teacherPageInfo = teacherService.queryByTeacherName(teacherQuery);
        boolean b;
        if (id != null) {
            b = teacherService.editTeacher(teacher);
        } else {
            b = teacherService.addTeacher(teacher);
        }
        return "redirect:/teacher/query";
    }

    @GetMapping("/update")
    public String update(Model model) {
        List<Department> departmentList=departmentService.queryAllDepartmentList();
        List<Major> majorList=majorService.queryAllMajorList();
        model.addAttribute("majorList",majorList);
        model.addAttribute("departmentList",departmentList);
        Teacher teacher = new Teacher();
        model.addAttribute("teacher", teacher);
        return "/teacher/editTeacher";
    }

    @GetMapping("/delete/{id}")
    public String deleteTeacherById(@PathVariable("id") Integer id) {
        boolean b = teacherService.deleteTeacherById(id);
        return "redirect:/teacher/query";
    }
}